package socket.protocol;

import lombok.extern.slf4j.Slf4j;
import org.smartboot.socket.Protocol;
import org.smartboot.socket.transport.AioSession;
import org.springframework.stereotype.Component;

import java.nio.ByteBuffer;

@Slf4j
@Component
public class StringProtocol implements Protocol<String> {

    /**
     * 消息解码
     *
     * @param readBuffer - 消息内容
     * @param session    - 当前操作人身份
     */
    @Override
    public String decode(ByteBuffer readBuffer, AioSession session) {
        int remaining = readBuffer.remaining();
        if (remaining < Integer.BYTES) {
            return null;
        }
//        readBuffer.mark();
//        int length = readBuffer.getInt();
//        if (length > readBuffer.remaining()) {
//            readBuffer.reset();
//            return null;
//        }
        byte[] b = new byte[remaining];
        readBuffer.get(b);
        readBuffer.mark();
        return new String(b);
    }
}